package com.appspot.crosslinky;

import java.util.List;
import java.util.logging.Logger;
import com.google.wave.api.*;

/**
 * 
 * @author jackpark
 * @author mark.carranza
 * 
 */
@SuppressWarnings("serial")
public class CrosslinkyServlet extends AbstractRobotServlet {

	private static final String CLASS_NAME = "CrossLinkyServlet";
	private static final String WAVE_ID = "waveID";
	private static final String WAVELET_ID = "waveletID";
	private static final String BLIP_ID = "blipID";
	private static final String LINK_ANNOTATION = "link/wave";

	private static final Logger log = 
		Logger.getLogger(CrosslinkyServlet.class.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.wave.api.AbstractRobotServlet#processEvents(com.google.wave
	 * .api.RobotMessageBundle)
	 */
	@Override
	public void processEvents(RobotMessageBundle bundle) {
		String logContext = CLASS_NAME + ".processEvents: ";
		Wavelet wavelet = bundle.getWavelet();
		Blip rootBlip = wavelet.getRootBlip();

		// more than one wavelet in a wave
		if (bundle.wasSelfAdded()) {
			log.info(logContext + " wasSelfAdded()");
		}

		// Handle various events.
		for (Event e : bundle.getEvents()) {
			log.info(logContext + e.getType().toString());
			Blip eventBlip = e.getBlip();
			Wavelet eventWavelet = e.getWavelet();			
			
			switch (e.getType()) {
			case FORM_BUTTON_CLICKED:
				log.info(logContext + "FORM_BUTTON_CLICKED");
				break;
			case BLIP_SUBMITTED:
				log.info(logContext + "BLIP_SUBMITTED");
				// get
				TextView tv = eventBlip.getDocument();
				FormView fv = tv.getFormView();
				List<Annotation> annotations = tv.getAnnotations(LINK_ANNOTATION);
				List<FormElement> formElements = fv.getFormElements();				
				List<Element> elements = tv.getElements();
				List<Blip> blips = tv.getInlineBlips();
				Integer as = new Integer(annotations.size());
				Integer vs = new Integer(blips.size());
				Integer es = new Integer(elements.size());
				Integer fes = new Integer(formElements.size());
				// tv.append("<br /> " + as.toString() + " annotations in this blip");
				
				GadgetView gv = tv.getGadgetView();
				List<Gadget> gadgets = gv.getGadgets();
				Integer gs = new Integer(gadgets.size());

				/*
				tv.append("<br /> " + es.toString() + " Inline Blips in this blip");
				tv.append("<br /> " + es.toString() + " elements in this blip");
				tv.append("<br /> " + fes.toString() + " form elements in this blip");
				tv.append("<br /> " + gs.toString() + " gadgets in this blip");
				*/
				// tv.append("<br />this wave ID: " + eventWavelet.getWaveId());
				// tv.append("<br />this wavelet ID: " + eventWavelet.getWaveletId());
				// tv.append("<br />this rootblip ID: " + eventWavelet.getRootBlipId());
				
				for (Annotation a : annotations) {
					// tv.append("<br />Name: " + a.getName());
					// tv.append("<br />Value: " + a.getValue());	
					Wavelet postWavelet = bundle.getWavelet(a.getValue(), "wavesandbox.com!conv+root");
					Blip postBlip = postWavelet.appendBlip();
					TextView postTv = postBlip.getDocument();
					String eventTitle = eventWavelet.getTitle(); 
					postTv.append(eventTitle);
					postTv.setAnnotation(
							new Range(0,eventTitle.length()), 
							LINK_ANNOTATION, 
							eventWavelet.getWaveId());
					// tv.append("<br />" + eventWavelet.getTitle() + " " + eventWavelet.getWaveId());					
				}						
				
				break;
			case DOCUMENT_CHANGED:
				log.info(logContext + "DOCUMENT_CHANGED");
				break;
			case WAVELET_BLIP_CREATED:
				log.info(logContext + "WAVELET_BLIP_CREATED");
				// we need to test this, then get the info from the blip
				// does this blip have a dataDocument?
				// display the data document in a new blip

				break;
			}
		}
	}

}
